<?php

/**
 * This is the model class for table "{{adm_group}}".
 *
 * The followings are the available columns in table '{{adm_group}}':
 * @property integer $zag_id
 * @property string $zag_title
 * @property string $name
 * @property integer $type
 * @property string $description
 * @property string $bizrule
 * @property string $data
 */
class AdmGroup extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return AdmGroup the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{adm_group}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('type', 'numerical', 'integerOnly'=>true),
			array('zag_title, name', 'length', 'max'=>100),
			array('description', 'length', 'max'=>255),
			array('bizrule, data', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zag_id, zag_title, name, type, description, bizrule, data', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'childs'=>array(self::HAS_MANY, 'AdmPrivilege', array('parent'=>'name')),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zag_id' => 'Zag',
			'zag_title' => 'Zag Title',
			'name' => 'Name',
			'type' => 'Type',
			'description' => 'Description',
			'bizrule' => 'Bizrule',
			'data' => 'Data',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zag_id',$this->zag_id);
		$criteria->compare('zag_title',$this->zag_title,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('type',$this->type);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('bizrule',$this->bizrule,true);
		$criteria->compare('data',$this->data,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	* 更新总是允许列表
	* @param $actions 数组  操作名称，格式：model-ControllerAction
	* @param $type 操作类型 1，添加 ; 2，减少
	*/
	public function updataAwayAllow($actions,$type=1){
		$paramAllowed = array();
		if(!is_file($this->getAlwaysAllowedFile())) {
			$handle = fopen($this->getAlwaysAllowedFile(), "wb");
			fwrite($handle, "<?php\n return array();\n?>");
			fclose($handle);
		}
		$guiAllowed = include($this->getAlwaysAllowedFile());
		if(!is_array($guiAllowed)){
			$guiAllowed = array();
		}
		if($type=='1'){
			$allowActions = array_merge($guiAllowed, $actions);
			$allowActions = array_unique($allowActions);
		}elseif($type=='2'){
			$allowActions =array_diff($guiAllowed,$actions);
		}
		$handle = fopen($this->getAlwaysAllowedFile(), "wb");
		fwrite($handle,"<?php \n return array(\n\t'" . implode("',\n\t'", $allowActions) . "'\n);\n?>");
		fclose($handle);
	}
	
   /**
    * 获取权限缓存文件
    */
   public function getAlwaysAllowedFile() {
   		return Yii::getPathOfAlias(Yii::app()->params->alwaysAllowedPath).DIRECTORY_SEPARATOR."foreveradm.php";
   }
   
   /**
    * 获取操作的备注名
    */
   public function getActionName($type){
	   	$criteria=new CDbCriteria;
	   
	   	$criteria->compare('name',$type);
	   	$action = $this->find($criteria);
	   	if(!empty($action)){
	   		return $action->zag_title;
	   	}else{
	   		return NULL;
	   	}
   }
   
   /**
    * 获取权限集列表
    */
   public function getPrivilege($type=null){
   	if($type==null){
   		$criteria=new CDbCriteria;
   		$criteria->compare('type','2');
   		$data = $this->findAll($criteria);
   		return CHtml::listData($data,'name','zag_title');
   	}else{
   		$level = $this->getPrivilege();
   		if(array_key_exists($type,$level)){
   			return $level[$type];
   		}
   	}
   }
   
   /**
    * 获取分配的权限集名
    */
   public function getTasksName($type){
	   	$type = trim($type);
	   	$criteria=new CDbCriteria;
	   	$criteria->compare('child',$type);
	   	$privilege = new AdmPrivilege();
	   	$Tasks = $privilege->find($criteria);
	   	if(!empty($Tasks)){
		   	$model = $this->findByAttributes(array('name'=>$Tasks->parent));
			if(!empty($model))
		   		return $model->attributes;
	   	}
   }
   
   /*
    * 获取总是允许列表
   * */
   public function getAllowedAccess() {
   	$paramAllowed = array();
   	if(!is_file($this->getAlwaysAllowedFile())) {
   		$handle = fopen($this->getAlwaysAllowedFile(), "wb");
   		fwrite($handle, "<?php\n return array();\n?>");
   		fclose($handle);
   	}
   	$guiAllowed = include($this->getAlwaysAllowedFile());
   	if(!is_array($guiAllowed)){
   		$guiAllowed = array();
   	}
   	return $guiAllowed;
   }
   
   /*
    * 创建新的权限集名称
   * */
   public function createTaskName($controller){
	   	//产生随机数
	   	$hash = create_randomstr();
	   	$name = trim($controller)."-".$hash;
	   	$criteria=new CDbCriteria;
	   	$criteria->compare('name',$name);
	   	$criteria->compare('type','2');
	   	if(!$this->exists($criteria)){
	   		return $name;
	   	}else{
	   		$this->createTaskName($controller);
	   	}
   }
   
   /*
    * 获取指定权限集的所有操作
   * */
   public function getTaskAction($task){
	   	$criteria=new CDbCriteria;
	   	$criteria->compare('name',$task);
	   	$model = $this->find($criteria);
	   	//$taskAction = CHtml::listData($model,'childs.child','childs.action.zmg_title');
	   	foreach($model->childs as $val){
	   		if(isset($val->action)){
	   			$taskAction[$val->child] = $val->action->zag_title;
	   		}
	   	}
	   	if(empty($model->childs))
	   		$taskAction = array();
	   	//print_r($taskAction);
	   	return $taskAction;
   }
}